<?php
/**
 * alltosun.com 通知 index.php
 * ============================================================================
 * 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址: http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明：这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 石武浩 (shiwh@alltosun.com) $
 * $Date:  2014-6-23 下午12:53:03 $
 * $Id$
*/

class Action
{
    private $per_page = 10;
    private $company_id;
    private $user_id;
    private $user_info;

    public function __construct()
    {
        $this->user_id    = user_helper::get_user_id();
        $this->company_id = company_helper::get_company_id();
        $this->user_info  = user_helper::get_user_info($this->user_id);

        if (!$this->user_id) {
            if (!Request::isAjax()) {
                throw new AnMessageException('对不起，请登录！', 'permission', 'user/login');
            }
        }
    }

    public function index()
    {
        $type         = Request::getParam('type', 1);
        $page         = Request::getParam('page_no', 1);

        $params = array();
        $params['type']         = $type;
        $params['to_user_id']   = $this->user_id;
        $params['page']         = $page;

        $list = _widget('notification')->get_list($params);

        // 更新状态
        if ($list) {
            foreach ($list as $k => $v) {
                    _model('notification')->update($v['id'], array('is_read'=>1));

                    if ($type == 1) {
                        message_helper::update_message_unread_num($this->user_id, 'system', 'clean', 0);
                    } else {
                        message_helper::update_message_unread_num($this->user_id, 'app', 'clean', 0);
                    }
            }
        }

        if (Request::isAjax() && $page > 1) {
            if ($count) {
                $response = Response::fetch('widget/list.html');
                return array('info' => 'ok', 'response' => $response);
            } else {
                return array('info' => 'empty');
            }
        } else {
            Response::display('index.html');
        }
    }

    /**
     * 是否确认 0,1,2
     */
    public function confirm()
    {
        $id         = Request::getParam('id', 0);
        $is_confirm = Request::getParam('is_confirm', 0);
        $info       = Request::getParam('info', array());

        if (!$id) {
            return '请选择您要修改的信息';
        }
        if (!$is_confirm) {
            return '您没有执行任何操作';
        }

        //@TODO 先只建立圈子
        if (isset($info) && $info['res_id'] && $info['to_user_id'] && $info['user_id']) {
            _model('notification')->update($id, array('is_confirm'=>1));
            // 查询以前是否加入过
            $circle_user_type = circle_helper::get_circle_user_type($info['res_id'], $info['to_user_id']);
            if (!$circle_user_type) {
                // 创建圈子
                circle_helper::add_circle_user($info['res_id'], $info['to_user_id'], 20);

                // 创建成功，发送通知
                $params = array();
                $params['company_id'] = $this->company_id;
                $params['res_name']   = 'system';
                $params['user_id']    = $this->user_id;
                $params['to_user_id'] = $info['user_id'];
                $user_info            = $this->user_info;
                $params['content']    = '<a href="javascript;">'.user_helper::display_name($user_info['id'], 'real_name').'</a> - 接受了您的邀请';

                _widget('notification')->create($params);
            }
        } else {
            _model('notification')->update($id, array('is_confirm'=>2));

            // 创建成功，发送通知
            $params = array();
            $params['company_id'] = $this->company_id;
            $params['res_name']   = 'system';
            $params['user_id']    = $this->user_id;
            $params['to_user_id'] = $info['user_id'];
            $user_info            = $this->user_info;
            $params['content']    = '<a href="javascript;">'.user_helper::display_name($user_info['id'], 'real_name').'</a> - 拒绝了您的邀请';

            _widget('notification')->create($params);
        }

        return 'ok';
    }

    /**
     * 删除通知
     */
    public function delete()
    {
        $id = Request::getParam('id');
        if (!$id) {
            return '请选择您要删除的信息！';
        }

        $ids = explode(',', trim($id, ','));
        foreach ($ids as $v) {
            $article_info = _uri('notification', $v);
            if (!$article_info) {
                continue;
            }

            if ($article_info['status'] == 0) {
                _model('notification')->delete($v);
            } else {
                _model('notification')->update($v, array('status'=>0));
            }
        }

        return "ok";
    }

    public function swh_test_add_notification()
    {
        $params = array(
                    'company_id'       => 0,
                    'res_name'         => 'circle',
                    'user_id'          => 1,
                    'to_user_id'       => 11,
                    'content'          => '邀请你加入圈子 <a>IT圈</a>'
                );
        $res = _widget('notification')->create($params);
        var_dump($res);
    }
}
?>